from datetime import datetime
from typing import Optional, List
from sqlalchemy import Column, Integer, String, DateTime, Float, JSON, ForeignKey, Text
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Content(Base):
    __tablename__ = 'contents'
    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8mb4',
        'mysql_collate': 'utf8mb4_unicode_ci'
    }
    
    id = Column(Integer, primary_key=True)
    platform = Column(String(50), nullable=False)
    content_type = Column(String(20), nullable=False)
    url = Column(String(500), nullable=False)
    title = Column(String(200))
    text = Column(Text)
    author = Column(String(100))
    publish_time = Column(DateTime)
    likes = Column(Integer)
    comments = Column(Integer)
    collections = Column(Integer)
    local_path = Column(String(500))
    
    media_files = relationship("MediaFile", back_populates="content", cascade="all, delete-orphan")
    
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    __table_args__ = (
        {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8mb4', 'mysql_collate': 'utf8mb4_unicode_ci'}
    )

class MediaFile(Base):
    __tablename__ = 'media_files'
    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8mb4',
        'mysql_collate': 'utf8mb4_unicode_ci'
    }
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    content_id = Column(Integer, ForeignKey('contents.id', ondelete='CASCADE'))
    file_type = Column(String(20, collation='utf8mb4_unicode_ci'))
    original_url = Column(String(500, collation='utf8mb4_unicode_ci'))
    local_path = Column(String(500, collation='utf8mb4_unicode_ci'))
    file_size = Column(Integer)
    
    content = relationship("Content", back_populates="media_files")
    created_at = Column(DateTime, default=datetime.utcnow) 